<template>
  <div class="charge-pilepower" ref="pilepower_ref"></div>
</template>

<script>
export default {
  props: {
    reportData: Object,
  },
  data() {
    return {
      chartInstance: null,
      allData: [], //服务器返回的数据
      currentPage: 1,
      totalPage: 0,
      timerId: null, //定时器的标识
    };
  },
  mounted() {
    this.initChart();
    this.getData();
  },
  destroyed() {
    clearInterval(this.timerId);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(
        this.$refs.pilepower_ref,
        "chalk"
      );
      // this.chartInstance = this.$echarts.init(this.$refs.pilepower_ref, "vintage");
      // 初始化配置的控制
      const initOption = {
        title: {
          text: "▎累计服务费用",
          left: 10,
          top: 10,
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, // 距离是包含坐标轴上的文字
        },
        xAxis: {
          type: "value",
          axisLabel: {
            formatter: "{value} 元",
            align: "center",
          },
        },
        yAxis: {
          type: "category",
        },
        tooltip: {
          trigger: "item",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              width: 20,
              color: "#2D3443",
            },
          },
        },
        series: [
          {
            type: "bar",
            barWidth: 20,
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              barBorderRadius: [0, 10, 10, 0],
              // 指明颜色渐变的方向
              // 指明不同百分比之下颜色的值
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                // 百分之0状态之下的颜色值
                {
                  offset: 0,
                  color: "#5052EE",
                },
                // 百分之100状态之下的颜色值
                {
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      const active_initOption = {};
      // 对图表对象进行鼠标事件的监听
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    getData() {
      this.allData = this.reportData.allData;
      this.totalPage = this.reportData.totalPage;
      this.updateChart();
      this.startInterval();
    },

    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const chargePileName = showData.map((item) => {
        return "充电桩" + item.pileId;
      });
      const chargePileValue = showData.map((item) => {
        return item.serviceBill;
      });
      const dataOption = {
        yAxis: {
          data: chargePileName,
        },
        series: [
          {
            data: chargePileValue,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
  },
};
</script>

<style lang="less" scoped>
.charge-pilepower {
  width: 360px;
  height: 240px;
  overflow: hidden;
  border-radius: 10px !important;
}
</style>
